<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>个人资料</title>
    <link media="all" rel="stylesheet" th:href="@{/layui/css/layui.css}"/>
    <link media="all" rel="stylesheet" th:href="@{/css/public.css}"/>
    <link media="all" rel="stylesheet" th:href="@{/lib/cropper/cropper.css}" />
    <link media="all" rel="stylesheet" th:href="@{/css/files.css}"/>
</head>
<body class="background-info">
<!--<h2 style="padding: 20px 20px"><b><a href="/"> 返回首页 </a></b></h2>-->

<form class="layui-form layui-row form-container" method="post" style="text-align: center">
    <div class="layui-col-md6 layui-col-xs12 user_right">
        <div class="layui-upload-list userFace">
            <input id="imgPath" name="imgPath" type="hidden" th:value="${session.user.imgPath}">
            <img class="layui-upload-img layui-circle userFaceBtn userAvatar" id="userFace"
                 th:src="${session.user.imgPath}" alt="头像"
                 th:data-default-src="'/images/default.jpeg'"
                 onerror="this.src=this.dataset.defaultSrc">
        </div>
        <button class="layui-btn layui-btn-primary userFaceBtn" id="changeAvatar" type="button">
            <i class="layui-icon">&#xe67c;</i> 掐指一算，我要换一个头像了
        </button>
        <p>由于是纯静态页面，所以只能显示一张随机的图片</p>

        <div class=" layui-col-xs12">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名：</label>
                <div class="layui-input-block">
                    <input class="id" name="id" th:value="${session.user.id}" type="hidden">
                    <input class="layui-input layui-disabled" disabled id="username" th:value="${session.user.username}" type="text"
                           value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码：</label>
                <div class="layui-input-block">
                    <input class="layui-input layui-disabled" disabled id="password" name="password" th:value="${session.user.password}"
                           type="password" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名：</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="name" th:value="${session.user.name}" type="text"
                           value="">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: left">
                <label class="layui-form-label">性别：</label>
                <div class="layui-input-block">
                    <input name="sex" th:checked="${session.user.sex == '1'}" title="男" type="radio" value="1">
                    <input name="sex" th:checked="${session.user.sex == '0'}" title="女" type="radio" value="0">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">年龄：</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="age" name="age" th:value="${session.user.age}" type="text" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户住址：</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="address" name="address" th:value="${session.user.address}" type="text"
                           value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">手机号：</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="phone" lay-verify="phone" name="phone" th:value="${session.user.phone}"
                           type="text" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">身份证：</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="idCard" name="idCard" th:value="${session.user.idCard}" type="text"
                           value="">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-filter="changeUser" lay-submit="">立即提交</button>
                </div>
            </div>
        </div>
    </div>
</form>

<script th:src="@{/layui/layui.js}"></script>

<script type="text/javascript">
    layui.use(function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;

        form.render();  // 初始化渲染表单

        // 点击更换头像按钮
        $('#changeAvatar').click(function () {
            // openAvatar();

            // 打开新窗口
            var uploadWindow = window.open('/avatarUpload', '_blank', 'width=800,height=600');

            // 监听子窗口的消息
            window.addEventListener('message', function(e) {
                if (e.origin !== window.location.origin) return;

                if (e.data.type === 'avatarUpdate') {
                    // 更新头像显示
                    $('#userFace').attr('src', e.data.path);
                    $('#imgPath').val(e.data.path);
                    layer.msg('头像更新成功');
                }
            });
        });

        // 提交个人资料
        form.on("submit(changeUser)", function (data) {
            var index = layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            $.post("/user/updateUser", data.field, function (res) {
                layer.close(index);
                layer.msg(res.msg);
                if (res.code === 200) {
                    layer.confirm('个人信息修改成功！是否立即返回登录界面重新登录？', {
                        icon: 1,
                        title: '操作确认',
                        btn: ['确认返回', '留在当前页'],
                        btnAlign: 'c'
                    }, function (confirmIndex) {
                        window.location.href = '/logout';
                        layer.close(confirmIndex);
                        layer.closeAll('dialog');
                    }, function () {
                        layer.msg('已取消返回', {
                            icon: 5,
                            time: 1500
                        });
                    });
                }
            });
            return false;
        });
    });
</script>

</body>
</html>